home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2005 March / Macworld CD March 2005 - Marathon Trilogy.iso / Shareware World / Text Processing / HexEdit Release.sit / HexEdit Release / Project / Source / Prefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-10-30  |  5.7 KB  |  262 lines  |  [TEXT/CWIE]

  1. /*
  2.  * The contents of this file are subject to the Mozilla Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/MPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * The Original Code is Copyright 1993 Jim Bumgardner.
  13.  * 
  14.  * The Initial Developer of the Original Code is Jim Bumgardner
  15.  * Portions created by Lane Roathe are
  16.  * Copyright (C) Copyright © 1996-2002.
  17.  * All Rights Reserved.
  18.  *
  19.  * Modified: $Date: 2004/09/10 04:05:47 $
  20.  * Revision: $Id: Prefs.c,v 1.13 2004/09/10 04:05:47 raving Exp $
  21.  *
  22.  * Contributor(s):
  23.  *        Lane Roathe
  24.  *        Nick Shanks
  25.  */
  26.  
  27. // 05/10/01 - GAB: MPW environment support
  28. #ifdef __MPW__
  29. #include "MPWIncludes.h"
  30. #endif
  31.  
  32. #include "Prefs.h"
  33. #include "Menus.h"
  34.  
  35. static Str63 prefsFName;
  36.  
  37. /*** INIT PREFS ***/
  38. static OSStatus _prefsInit( void )
  39. {
  40.     // bad version, create default
  41.     memset( &gPrefs, 0, sizeof( gPrefs ) );
  42.  
  43.     gPrefs.searchCase = false;
  44.     gPrefs.searchMode = EM_Ascii;
  45.     gPrefs.searchSize = CM_Byte;
  46.     gPrefs.searchType = CM_Different;
  47.     gPrefs.searchForward = true;
  48.  
  49.     gPrefs.asciiMode = false;
  50.     gPrefs.decimalAddr = EM_Hex;
  51.     gPrefs.overwrite = false;
  52.     gPrefs.nonDestructive = true;
  53.     gPrefs.backupFlag = true;
  54.     gPrefs.vertBars = true;
  55.  
  56.     gPrefs.useColor = true;
  57.  
  58.     gPrefs.csMenuID = 3;    // default is 1'st color in menu (" Default")
  59.     gPrefs.csResID = -1;
  60.  
  61.     gPrefs.constrainSize = true;
  62.     gPrefs.formatCopies = true;
  63.  
  64.     gPrefs.version = PREFS_VERSION;
  65.     return noErr;
  66. }
  67.  
  68. /*** GET PREFS FILE PATH ***/
  69. static Boolean _prefsGetFSPath( long *prefDirID, short *systemVolRef )
  70. {
  71.     OSStatus    error;
  72.     
  73.     error = FindFolder( kOnSystemDisk, kPreferencesFolderType, kCreateFolder, 
  74.         systemVolRef, prefDirID );
  75.     if( error != noErr )
  76.         return false;
  77.     
  78.     return true;
  79. }
  80.  
  81. /*** WRITE PREFS ***/
  82. static OSStatus _prefsWrite( long *prefDirID, short *systemVolRef )
  83. {
  84.     OSStatus        error;
  85.     short        fileRefNum;
  86.     long        byteCount;
  87.     FSSpec        theSpecs;
  88.     
  89.     error = FSMakeFSSpec( *systemVolRef, *prefDirID, prefsFName, &theSpecs );
  90.     if( error != noErr )
  91.     {
  92.         if( error != fnfErr )
  93.         {
  94. //             ErrorExit( "\pPrefs FSMakeFSSpec() Error", error );
  95.             return error;
  96.         }
  97.         error = FSpCreate( &theSpecs, kPrefCreatorType, kPrefFileType, smSystemScript );
  98.         if( error != noErr )
  99.         {
  100. //             ErrorExit( "\pPrefs FSpCreate() Error", error );
  101.             return error;
  102.         }
  103.     }
  104.     error = FSpOpenDF( &theSpecs, fsRdWrPerm, &fileRefNum );
  105.     if( error != noErr )
  106.     {
  107. //         ErrorExit( "\pPrefs FSpOpenDF() Error", error );
  108.         return error;
  109.     }
  110.     
  111.     byteCount = sizeof( gPrefs );
  112.     
  113.     error = FSWrite( fileRefNum, &byteCount, (Ptr) &gPrefs );
  114.     if( error != noErr )
  115.     {
  116. //         ErrorExit( "\pPrefs FSWrite() Error", error );
  117.         return error;
  118.     }
  119.     
  120.     error = FSClose( fileRefNum );
  121.     if( error != noErr )
  122.     {
  123. //         ErrorExit( "\pPrefs FSClose() Error", error );
  124.         return error;
  125.     }
  126.     
  127.     return error;
  128. }
  129.  
  130. /*** READ PREFS ***/
  131. static OSStatus _prefsRead( long *prefDirID, short *systemVolRef )
  132. {
  133.     OSStatus    error;
  134.     short        fileRefNum;
  135.     long        byteCount;
  136.     FSSpec        theSpecs;
  137.     
  138.     error = FSMakeFSSpec( *systemVolRef, *prefDirID, prefsFName, &theSpecs );
  139.     if( error != noErr )
  140.     {
  141. //         if( error == fnfErr )
  142.             return error;
  143. //         else
  144. //         {
  145. //             ErrorExit( "\pPrefs FSMakeFSSpec() Error", error );
  146. //         }
  147.     }
  148.     
  149.     error = FSpOpenDF( &theSpecs, fsRdWrPerm, &fileRefNum );
  150.     if( error != noErr )
  151.     {
  152. //         ErrorExit( "\pPrefs FSpOpenDF() Error", error );
  153.         return error;
  154.     }
  155.     
  156.     byteCount = sizeof( gPrefs );
  157.     
  158.     error = FSRead( fileRefNum, &byteCount, (Ptr) &gPrefs );
  159.     if( error != noErr )
  160.     {
  161.         if( error == eofErr )
  162.             FSClose( fileRefNum );
  163.         else
  164.         {
  165. //             ErrorExit( "\pPrefs FSRead() Error", error );
  166.         }
  167.         return error;
  168.     }
  169.     
  170.     error = FSClose( fileRefNum );
  171.     if( error != noErr )
  172.     {
  173. //         ErrorExit( "\pPrefs FSClose() Error", error );
  174.         return error;
  175.     }
  176.     
  177.     return error;
  178. }
  179.  
  180. /*** DELETE PREFS ***/
  181. static Boolean _prefsDelete( long *dirID, short *volRef )
  182. {
  183.     FSSpec        theSpecs;
  184.     OSStatus    error;
  185.     
  186.     error = FSMakeFSSpec( *volRef, *dirID, prefsFName, &theSpecs );
  187.     if( error != noErr )
  188.         return false;
  189.     else
  190.         error = FSpDelete( &theSpecs );
  191.     
  192.     if( error != noErr )
  193.         return false;
  194.     
  195.     return true;
  196. }
  197.  
  198. #pragma mark -
  199.  
  200. /*** SAVE PREFS ***/
  201. Boolean PrefsSave( void )
  202. {
  203.     long        prefDirID;
  204.     short        systemVolRef;
  205.  
  206.     if( !_prefsGetFSPath( &prefDirID, &systemVolRef ) )
  207.         return false;
  208.  
  209.     // LR: v1.6.5 don't save a bad gPrefs structure!
  210.     if( PREFS_VERSION != gPrefs.version )
  211.         _prefsInit();
  212.  
  213. // LR: v1.6.5    gPrefs.csResID = gPrefs.csMenuID;    // LR: 1.5, yech!
  214. // LR: v1.6.5    gPrefs.version = PREFS_VERSION;
  215.  
  216.     if( noErr != _prefsWrite( &prefDirID, &systemVolRef ) )
  217.         return false;
  218.  
  219.     return true;
  220. }
  221.  
  222. /*** LOAD PREFS ***/
  223. Boolean PrefsLoad( void )
  224. {
  225.     OSStatus    error;
  226.     long        prefDirID;
  227.     short        systemVolRef;
  228.     Boolean        noProblems;
  229.  
  230.     GetIndString( prefsFName, kPrefsStringsID, kPrefsFileNameIndex );    // LR: v1.6.5 (always run, so use by Save is OK!)
  231.  
  232.     gPrefs.version = 0;    // failure flag
  233.  
  234.     noProblems = _prefsGetFSPath( &prefDirID, &systemVolRef );
  235.     if( noProblems )
  236.     {
  237.         error = _prefsRead( &prefDirID, &systemVolRef );
  238.         if( error == eofErr )
  239.         {
  240.             noProblems = _prefsDelete( &prefDirID, &systemVolRef );
  241. // LR: v1.6.5            return false;
  242.         }
  243.     }
  244. /* LR -- 1.5 gPrefs never worked...sigh
  245.     else if( error != noErr )
  246.         return false;
  247. */
  248.     if( PREFS_VERSION != gPrefs.version )    // check for good version #
  249.     {
  250.         _prefsInit();                    // !!! start from scratch!
  251.     }
  252.  
  253.     // funky...but menus sorted by name mess me up!
  254. // LR: v1.6.5    gPrefs.version = gPrefs.csResID;
  255.     gPrefs.csResID = GetColorMenuResID( gPrefs.csMenuID );
  256.  
  257.     if( gPrefs.asciiMode )    g.highChar = 0xFF;
  258.     else                    g.highChar = 0x7F;
  259.     return true;
  260. }
  261.  
  262.